package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.Recommend;
import com.burgeon.backend.SHOP.entity.RecommendConfig;
import com.burgeon.backend.SHOP.entity.RecommendSwitch;
import com.burgeon.backend.SHOP.entity.RecommendUser;
import com.burgeon.backend.SHOP.mappers.RecommendMapper;
import com.burgeon.backend.SHOP.service.GoodsService;
import com.burgeon.backend.SHOP.service.RecommendService;
import com.burgeon.backend.SHOP.service.SystemService;
import com.burgeon.backend.SHOP.service.ex.ErrorDataExceptionApp;
import com.burgeon.backend.WeChat.Wxacode;
import com.burgeon.backend.entity.TemplateSwitch;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.CouponMapper;
import com.burgeon.backend.service.QueueService;
import com.burgeon.backend.service.ex.UpDataException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class RecommendServiceImpl implements RecommendService {
    private final static Log logger = LogFactory.getLog(RecommendService.class);
    @Autowired
    private RecommendMapper recommendMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private QueueService queueService;
    @Autowired
    private Wxacode wxacode;
    @Autowired
    private SystemService systemService;


    /**
     * 获取推荐的优惠值
     * @param token
     * @return
     */
    @Override
    public RecommendSwitch findRecommendSwitch(String token) {
        RecommendSwitch recommendSwitch = recommendMapper.findRecommendSwitch(token);
        String coupon = recommendMapper.findCouponById(recommendSwitch.getRValue1());
        System.err.println(coupon);
        recommendSwitch.setRValue1(coupon);
        String newCoupon = recommendMapper.findCouponById(recommendSwitch.getRValue4());
        System.err.println(newCoupon);
        recommendSwitch.setRValue4(newCoupon);
        return recommendSwitch;
    }

    /**
     * 新用户注册成功
     * @param presentee
     * @param referrer
     * @param token
     * @return
     */
    @Override
    public Integer addRecommend(String presentee, String referrer, String token) {
        logger.info("========================================================"+referrer);
        if(null == referrer || referrer.isEmpty()){
            logger.info("推荐人信息没有获取到======================================="+referrer);
            throw new UpDataException("推荐人信息没有获取到"+referrer);
        }
        String account = accountMapper.GetAccountForToken(token);
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = sdf.format(now);
        Integer row = recommendMapper.addRecommend(presentee, referrer, token, account,dateString);
        if(row!=1){
           throw new ErrorDataExceptionApp("创建奖励数据失败");
        }
        //推荐获取的值
        RecommendSwitch recommendSwitch = recommendMapper.findRecommendSwitch(token);
        //推荐人的用户信息
        RecommendUser recommendUser = recommendMapper.findUserByReferrer(referrer);
        //推荐获取优惠券
        if("Y".equals(recommendSwitch.getRSwitch1())){
            queueService.putRecommendUserCoupon(recommendSwitch.getRValue1(), recommendUser.getPhone(), token);
        }
        //推荐获取积分
        if("Y".equals(recommendSwitch.getRSwitch2())){
            queueService.putRecommendUserIntegral(recommendUser.getName(),recommendUser.getPhone(),recommendUser.getOpenid(),
                    recommendSwitch.getRValue2(), token);
        }
        return row;
    }

    /**
     * 获取推荐人小程序码
     * @param uid
     * @param token
     * @return
     */
    @Override
    public String recommendCode(String uid, String token) {
        String code = recommendMapper.getRecommendCode(uid);
        if(code == null){
            String accessToken = systemService.GetAccesToken(token);
            code = wxacode.recommendCode(uid,accessToken,token);
            recommendMapper.addRecommendCode(uid,code,token);
        }
        return code;
    }

    /**
     * 获取推荐页面的信息
     * @param token
     * @return
     */
    @Override
    public RecommendConfig getRecommendConfigByToken(String token, HttpSession session) {
        //获取推荐奖励信息
        RecommendSwitch recommendSwitch = findRecommendSwitch(token);
        //获取推荐配置信息
        RecommendConfig recommendConfig = recommendMapper.getRecommendConfigByToken(token);
        if(recommendConfig == null){
            RecommendConfig recommendConfig1 = new RecommendConfig();
            recommendConfig1.setRecommendSwitch(recommendSwitch);
            if (null != session.getAttribute("uid")) {
                String referrerId = session.getAttribute("uid").toString();
                recommendConfig1.setReferrerId(referrerId);
            }
            return recommendConfig1;
        }
        recommendConfig.setRecommendSwitch(recommendSwitch);

        if (null != session.getAttribute("uid")) {
            String referrerId = session.getAttribute("uid").toString();
            recommendConfig.setReferrerId(referrerId);
        }


        return recommendConfig;
    }

    /**
     * 获取推荐的开关和值（优惠券和积分）
     * @param account
     * @return
     */
//    @Override
//    public RecommendSwitch findRecommendSwitchByAccount(String account) {
//        RecommendSwitch recommendSwitch = recommendMapper.findRecommendSwitchByAccount(account);
//        recommendSwitch.setRSwitch1(recommendSwitch.getRSwitch1().equals("Y") ? "1" : "2");
//        recommendSwitch.setRSwitch4(recommendSwitch.getRSwitch4().equals("Y") ? "1" : "2");
//
////        couponMapper.findCouponTypeById(recommendSwitch.getRValue1());//推荐优惠券
////        couponMapper.findCouponTypeById(recommendSwitch.getRValue4());//新用户优惠券
//        return recommendSwitch;
//    }
//
//    @Override
//    public Integer updateRecommendSwitch(String rSwitch1, String rValue1, String rSwitch2, String rValue2, String rSwitch3, String rValue3, String rSwitch4, String rValue4, String account) {
//        return null;
//    }
//
//    @Override
//    public ArrayList<Recommend> getRecommend(String account) {
//        return null;
//    }
}
